<template>
	<view style="padding: 200rpx;">
		<button @tap="initAPP()" type="primary" size="mini">初始化</button>
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app'
import XBCCS from 'xbccs'

// 初始化
function initAPP(code = '') {
	if(code){
		uni.setStorageSync('__xbccs_demo_app_code__', code)
	}else{
		code = uni.getStorageSync('__xbccs_demo_app_code__')
	}
	
	try{
		uni.showLoading({
			title: '模块初始化中'
		})
		
		XBCCS.init({
			client: 'app',
			userId: '84c5de665f9e42e02a3de826fca49fb3',
			code,
			goodsList: [
				{
					id: 1,
					name: '1金龙鱼食用油添加25%初榨橄榄 食用植物调和油5L（新老配方随机发货）',
					price: 99,
					originPrice: 139.99,
					imageUrl: 'https://img2.baidu.com/it/u=3970917306,880187556&fm=253&fmt=auto&app=120&f=JPEG?w=655&h=80882',
					url: 'https://www.baidu.com'
				},
				{
					id: 1,
					name: '2金龙鱼食用油添加25%初榨橄榄 食用植物调和油5L（新老配方随机发货）',
					price: 99,
					originPrice: 139.99,
					imageUrl: 'https://img2.baidu.com/it/u=3970917306,880187556&fm=253&fmt=auto&app=120&f=JPEG?w=655&h=80882',
					url: 'https://www.baidu.com'
				},
				{
					id: 1,
					name: '3金龙鱼食用油添加25%初榨橄榄 食用植物调和油5L（新老配方随机发货）',
					price: 99,
					originPrice: 139.99,
					imageUrl: 'https://img2.baidu.com/it/u=3970917306,880187556&fm=253&fmt=auto&app=120&f=JPEG?w=655&h=80882',
					url: 'https://www.baidu.com'
				},
				{
					id: 1,
					name: '4金龙鱼食用油添加25%初榨橄榄 食用植物调和油5L（新老配方随机发货）',
					price: 99,
					originPrice: 139.99,
					imageUrl: 'https://img2.baidu.com/it/u=3970917306,880187556&fm=253&fmt=auto&app=120&f=JPEG?w=655&h=80882',
					url: 'https://www.baidu.com'
				}
			],
			orderList: [
				{
					id: 1,
					name: '1金龙鱼食用油添加25%初榨橄榄 食用植物调和油5L（新老配方随机发货）',
					amount: 99,
					quantity: 1,
					imageUrl: 'https://img2.baidu.com/it/u=3970917306,880187556&fm=253&fmt=auto&app=120&f=JPEG?w=655&h=80882',
					url: 'https://www.baidu.com',
					orderNo: '254515154754545',
					createTime: '2021-01-01 12:00:00'
				},
				{
					id: 1,
					name: '2金龙鱼食用油添加25%初榨橄榄 食用植物调和油5L（新老配方随机发货）',
					amount: 99,
					quantity: 1,
					imageUrl: 'https://img2.baidu.com/it/u=3970917306,880187556&fm=253&fmt=auto&app=120&f=JPEG?w=655&h=80882',
					url: 'https://www.baidu.com',
					orderNo: '254515154754545',
					createTime: '2021-01-01 12:00:00'
				},
				{
					id: 1,
					name: '3金龙鱼食用油添加25%初榨橄榄 食用植物调和油5L（新老配方随机发货）',
					amount: 99,
					quantity: 1,
					imageUrl: 'https://img2.baidu.com/it/u=3970917306,880187556&fm=253&fmt=auto&app=120&f=JPEG?w=655&h=80882',
					url: 'https://www.baidu.com',
					orderNo: '254515154754545',
					createTime: '2021-01-01 12:00:00'
				},
			]
		}).then(() => {
			console.log('登录成功创建会话')
			// 登录成功创建会话
			XBCCS.createConversation().then(conversation => {
				uni.navigateTo({
					url: `/pages/chat?id=${conversation.conversationId}`
				})
			}).catch((err) => {
				console.log('会话创建失败', err)
			})
		}).catch((err) => {
			console.log('初始化失败', err)
		})
	}finally{
		uni.hideLoading()
	}
}

onLoad(({ code }) => {
	code && initAPP(encodeURIComponent(code))
})
</script>

<style>
</style>